# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=lensfun
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.3.3
pkgrel=5
pkgdesc="Database of photographic lenses and a library that allows advanced access to the database (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://lensfun.github.io/"
license=("LGPL3")
makedepends=("${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             # "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/lensfun/lensfun/archive/v${pkgver}.tar.gz
        lensfun-update-data.patch
        cmake-mingw.patch
        fix-cpp11-warnings.patch
        lensfun-update-data-exception.patch::https://github.com/lensfun/lensfun/commit/56813fd86c7a78c3f3fcb823dc4b2174c7bcbe67.diff)
sha256sums=('57ba5a0377f24948972339e18be946af12eda22b7c707eb0ddd26586370f6765'
            '8e416bf7f57cc06f765c868ce03a8fcb1a240b01f7afd972d51bba47c0d739bb'
            '459afdaaed4f1ea1c653fe7dc7c460303a8e49f6a2f15d0d66c18a3466ea2e6b'
            '0cb1386be1ba04d7a6cd7f5f128bf1e4eb03249ec0c4ad40ebe203666b157537'
            '471ff69e3e0ba425a28c3b37930b79d6deadcf50a0794c75015ab09e1979f239')

prepare(){
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/cmake-mingw.patch"
  patch -p1 -i "${srcdir}/lensfun-update-data.patch"

  # https://github.com/lensfun/lensfun/commit/694542f.patch
  patch -p1 -i "${srcdir}/fix-cpp11-warnings.patch"

  # https://github.com/lensfun/lensfun/pull/1014
  patch -p1 -i "${srcdir}/lensfun-update-data-exception.patch"
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi 

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DSETUP_PY_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"Ninja" \
      "${extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DSETUP_PY_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_STATIC=OFF \
      -DBUILD_AUXFUN=ON \
      -DBUILD_LENSTOOL=ON \
      -DBUILD_TESTS=OFF \
      -DBUILD_DOC=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/lgpl-3.0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-libs"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/gpl-3.0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-apps"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/cc-by-sa-3.0.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-data"
}
